/*
 * Copyright 2010 by BCV (Lausanne, Switzerland)
 * All rights reserved.
 *
 *  Author: ELCA Informatique SA
 *  Version:  $Revision: $
 */

package tcln.gxtx.client.widget.grid.renderer.html;

import tcln.gxtx.client.widget.treegrid.model.AbstractTreeModel;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;

/**
 * A simple implementation of {@link HtmlGridCellRenderer} which generates a DIV containing text data. Text data can be
 * customized.
 * 
 * @param <M>
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class SimpleTextHtmlGridCellRenderer<M extends AbstractTreeModel> extends AbstractHtmlGridCellRenderer<M> {
    private static final String EMPTY_STRING = "";

    private String textColor = "";
    private HorizontalAlignment textAlignment = HorizontalAlignment.LEFT;

    /**
     * @param ownerColumn
     */
    public SimpleTextHtmlGridCellRenderer(ColumnConfig ownerColumn) {
        super(ownerColumn);
    }

    /**
     * Sets new value for <code>textAlignment</code>.
     * 
     * @param textAlignment
     *            the new textAlignment to set
     */
    public void setTextAlignment(HorizontalAlignment textAlignment) {
        if (textAlignment == null) {
            textAlignment = HorizontalAlignment.LEFT;
        }
        this.textAlignment = textAlignment;
    }

    /**
     * Sets new value for <code>textColor</code>.
     * 
     * @param textColor
     *            the new textColor to set
     */
    public void setTextColor(String textColor) {
        this.textColor = String.valueOf(textColor);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String render(M model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<M> store,
            Grid<M> grid) {
        StringBuilder builder = new StringBuilder();
        builder.append("<div class=\"" + specificCssClass(model) + "\" style=\"");

        // change alignment
        builder.append("text-align: ").append(textAlignment.name().toLowerCase()).append("; ");

        // change text color
        if (textColor != null && !EMPTY_STRING.equals(textColor)) {
            builder.append("color: ").append(textColor).append("; ");
        }

        // set margin
        if (margins == null) {
            margins = new Margins();
        }
        builder.append("margin-top: ").append(margins.top).append("px; ");
        builder.append("margin-left: ").append(margins.left).append("px; ");
        builder.append("margin-bottom: ").append(margins.bottom).append("px; ");
        builder.append("margin-right: ").append(margins.right).append("px; ");

        builder.append("\">");

        // format displayed value
        String stringVal = formatText(model, property);
        builder.append(stringVal);

        // finalize
        builder.append("</div>");

        return builder.toString();
    }

    protected String formatText(M model, String property) {
        return dataFormatter.format(model, property);
    }

    /**
     * Customize specific CSS class for render component.
     * 
     * @param model
     * @return specific css style name.
     */
    protected String specificCssClass(M model) {
        return "";
    }
}
